/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.minhphuong.dal;

import org.hibernate.Session;
import com.minhphuong.helper.*;
import java.util.List;
import org.hibernate.Query;

/**
 *
 * @author QuocMinh
 */
public class HelperDAL {

    public static Session GetSession() {
        Helper helper = new Helper();
        return helper.session;
    }

    public static int getNextID(String table, String column) {
        int nextid = 0;

        Session session = null;
        try {
            session = HelperDAL.GetSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            String query = "select max(t." + column + ") from " + table + " as t order by t." + column + " desc";
            Query q = session.createQuery(query);
            List result = q.list();
            nextid = Integer.parseInt(result.get(0).toString()) + 1;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return nextid;
    }
}
